package com.yto.hrreport.event.buttons {
	
	import com.yto.hrreport.event.buttons.superbutton.ButtonEvent;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.Panel;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Tree;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class SysDeleteButtonEvent extends ButtonEvent {
		
		private static var deleteEvent : SysDeleteButtonEvent = new SysDeleteButtonEvent();
		private static var parentName : String;
		private var selecteItem : Object;
		
		private var txtNodeCode : TextInput;
		private var txtNodeId : TextInput;
		private var txtNodeName : TextInput;
		private var txtParentNodeCode : TextInput;
		private var chkIsParent : CheckBox;
		private var sysTree : Tree;
		private var headCanvas : Canvas;
		public function SysDeleteButtonEvent() {
			super();
		}
		
		public static function getInstance(parentName : String) : SysDeleteButtonEvent {
			SysDeleteButtonEvent.parentName = parentName;
			return deleteEvent;
		}
		
		public function deleteEvent() : void {
			
			var parentCanvas : Canvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas"));
			headCanvas = Canvas(parentCanvas.getChildByName(parentName + "_headCanvas"));
			var bodyCanvas : Canvas = Canvas(parentCanvas.getChildByName(parentName + "_bodyCanvas"));
			var hdivBox : HDividedBox = HDividedBox(bodyCanvas.getChildByName(parentName + "_hdivBox"));
			sysTree = Tree(hdivBox.getChildByName(parentName + "_sysTree"));
			var sysPanel : Panel = Panel(hdivBox.getChildByName(parentName + "_sysFunc"));
			
			selecteItem = sysTree.selectedItem;
//			if(selecteItem == null) {
//				Alert.show("请选择末级节点！" , "提示" , Alert.OK , hrreport.ui);
//				return ;
//			}
			
			if(sysPanel == null) {
				sysPanel  = Panel(hdivBox.getChildByName(parentName + "_sysTemplate"));
				
				
			} else {
				var sysCanvas : Canvas = Canvas(sysPanel.getElementAt(0));
				
				txtNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeCode"));
				txtNodeId = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeId"));
				txtNodeName = TextInput(sysCanvas.getChildByName(parentName + "_txtNodeName"));
				txtParentNodeCode = TextInput(sysCanvas.getChildByName(parentName + "_txtParentNodeCode"));
				chkIsParent = CheckBox(sysCanvas.getChildByName(parentName + "_chkIsParent"));
				
				Alert.show("确定删除此节点？" , "提示" , Alert.OK | Alert.CANCEL , hrreport.ui , checkDelete);
			}
			
		}
		
		public function checkDelete(event : CloseEvent) : void {
			
			if(event.detail == Alert.OK) {
				PopUpManager.addPopUp(hrreport.waitWindow , hrreport.ui , true);
				PopUpManager.centerPopUp(hrreport.waitWindow);
				
				var nodeServer : RemoteObject = new RemoteObject;
				nodeServer.destination = IProcessor.NodeProcessor;
				nodeServer.deleteNode(selecteItem.pk_nodes);
				nodeServer.addEventListener(ResultEvent.RESULT , deleteNode);
			}
		}
		
		private function deleteNode(event : ResultEvent) : void {
			
			var result : String = event.result as String;
			if(result == "1") {
				
				sysTree.enabled = true;
				txtNodeCode.enabled = false;
				txtNodeId.enabled = false;
				txtNodeName.enabled = false;
				txtParentNodeCode.enabled = false;
				chkIsParent.enabled = false;
				
				var nodeServer : RemoteObject = new RemoteObject;
				nodeServer.destination = IProcessor.NodeProcessor;
				nodeServer.exsitsTemplate(selecteItem.nodeid);
				nodeServer.addEventListener(ResultEvent.RESULT , exsitsTemplate);
				
			} else {
				PopUpManager.removePopUp(hrreport.waitWindow);
				Alert.show("删除时遇到未知错误，删除失败！" , "提示" , Alert.OK , hrreport.ui);
			}
			
		}
		
		private function exsitsTemplate(event : ResultEvent) : void {
			
			var result : String = event.result as String;
			
			if(result == "true") {
				Alert.show("该节点下存在单据模板，是否删除单据模板？" , "提示" , Alert.OK | Alert.CANCEL , hrreport.ui , checkDeleteTemplate);
			} else {
				updateButtons(null , "sysEdit" , headCanvas , false); 
				RefreshButtonEvent.getInstance(parentName).refreshEvent();
				
				PopUpManager.removePopUp(hrreport.waitWindow);
			}
			
		}
		
		private function checkDeleteTemplate(event : CloseEvent) : void {
			
			if(event.detail == Alert.OK) {
				var nodeServer : RemoteObject = new RemoteObject;
				nodeServer.destination = IProcessor.NodeProcessor;
				nodeServer.deleteTemplate(selecteItem.nodeid);
				nodeServer.addEventListener(ResultEvent.RESULT , deleteTemplate);
			} else {
				PopUpManager.removePopUp(hrreport.waitWindow);
			}
			
		}
		
		private function deleteTemplate(event : ResultEvent) : void {
			
			var result : String = event.result as String;
			
			if(result == "true") {
				updateButtons(null , "sysEdit" , headCanvas , false); 
				RefreshButtonEvent.getInstance(parentName).refreshEvent();
			} else {
				Alert.show("删除时遇到未知错误，删除失败！" , "提示" , Alert.OK , hrreport.ui);
			}
			
			PopUpManager.removePopUp(hrreport.waitWindow);
				
		}
		
	}
}